package com.ltv.xiangqiplugin.controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import com.ltv.xiangqiplugin.models.MoveStack;
import com.ltv.xiangqiplugin.models.PositionStack;
import com.ltv.xiangqiplugin.views.BoardView;
import com.ltv.xiangqiplugin.views.ChessApp;
import com.ltv.xiangqiplugin.views.GameActionView;
import com.ltv.xiangqiplugin.views.HtmlView;
import com.ltv.xiangqiplugin.views.MoveStackView;

public class GameActionControl implements ActionListener {
	private static GameActionControl instance;
	
	private GameActionControl() {
		PositionStack.getInstance().addPostion(BoardView.defaultBoardView().getGame().getPosition());
		PositionStack.getInstance().addObserver(BoardView.defaultBoardView());
		
		MoveStack.getInstance().addMove("start");
		MoveStack.getInstance().addVnMove("start");
		MoveStack.getInstance().addObserver(MoveStackView.getInstance());
	}
	
	@Override
	public void actionPerformed(ActionEvent event) {
		String command = event.getActionCommand();
		
		// Fix bug
		BoardViewControl.defaultControl().setOriginPanel(null);
		
		if (command.compareTo("NEXT") == 0) {
			PositionStack.getInstance().next();
			MoveStack.getInstance().next();
		} else if (command.compareTo("PREV") == 0) {
			PositionStack.getInstance().prev();
			MoveStack.getInstance().prev();
		} else if (command.compareTo("LAST") == 0) {
			PositionStack.getInstance().last();
			MoveStack.getInstance().last();
		} else if (command.compareTo("FIRST") == 0) {
			PositionStack.getInstance().first();
			MoveStack.getInstance().first();
		} else if (command.compareTo("ROLLBACK") == 0) {
			PositionStack.getInstance().setPositions(ChessApp.getBufferedPositions());
			MoveStack.getInstance().setMoves(ChessApp.getBufferedMoves());
			MoveStack.getInstance().setVnMoves(ChessApp.getBufferedVnMoves());
			
			PositionStack.getInstance().setIndex(ChessApp.getBufferedIndex());
			MoveStack.getInstance().setIndex(ChessApp.getBufferedIndex());
			
			PositionStack.getInstance().notifyObservers();
			MoveStack.getInstance().notifyObservers(true);
			
			GameActionView.getInstance().disableRollbackButton();
		}  else if (command.compareTo("REVERSE") == 0) {
			BoardView.defaultBoardView().setReverse(!BoardView.defaultBoardView().isReverse());
		}  else if (command.compareTo("HTML") == 0) {
			Vector<String> moves = MoveStack.getInstance().getMoves();
			
			int size = moves.size();
			String moveList = "";
			for (int i = size - 2; i >= 0; i--) {
				moveList += moves.get(i) + "+";
			}
			
			StringBuffer moveListUpdated = new StringBuffer();
		    
		    for (int i = 0; i < moveList.length() - 1; i++) {
		    	char c = moveList.charAt(i);
		    	if (isNum(c)) {
		    		moveListUpdated.append(Integer.parseInt("" + c));
		    	} else {
		    		moveListUpdated.append(c);
		    	}
		    }
		    
		    moveList = moveListUpdated.toString().toUpperCase();
		    
		    String html = "<applet archive=\"http://locpx-demo.appspot.com/views/plugin/1.0.0/xiangqiplugin.jar\" code=\"com.ltv.xiangqiplugin.views.ChessApp.class\" width=432 height=476>";
		    html += "\n";
		    html += "<param name=moveList value=\"" + moveList + "\"/>";
		    html += "\n";
		    html += "</applet>";
		    
		    HtmlView.getInstance().showText(html);
		    
		    System.out.println(html);
		}
		
	}
	
	private boolean isNum(char c) {
		return c >= '0' && c <= '9';
	}

	public static GameActionControl getInstance() {
		if (instance == null) {
			instance = new GameActionControl();
		}
		
		return instance;
	}

}
